package com.example.hissys.service;

import com.example.hissys.dto.NotificationDetailDTO;
import com.example.hissys.dto.NotificationRequest;
import com.example.hissys.mapper.NotificationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class NotificationService {
    @Autowired
    private NotificationMapper notificationMapper;

    /**
     * 获取用户的所有通知
     */
    public List<NotificationDetailDTO> getUserNotifications(Integer userId) {
        log.info("Fetching notifications for user: {}", userId);
        return notificationMapper.findByUserId(userId);
    }

    /**
     * 将通知标记为已读
     */
    public boolean markNotificationAsRead(Integer notificationId, Integer userId) {
        log.info("Marking notification {} as read for user {}", notificationId, userId);
        return notificationMapper.markAsRead(notificationId, userId) > 0;
    }

    @Transactional
    public void createNotification(NotificationRequest request) {
        log.info("Creating notification for user: {}", request.getUserId());
        
        int created = notificationMapper.create(request);
        if (created == 0) {
            throw new RuntimeException("创建通知失败");
        }
        
        log.info("Successfully created notification for user: {}", request.getUserId());
    }
} 